@echo off
echo ================================
echo    前端文件部署脚本
echo ================================

:: 设置变量
set NGINX_DIR=E:\Program Files (x86)\nginx-1.25.3
set PROJECT_DIR=%~dp0

echo 项目目录: %PROJECT_DIR%
echo Nginx目录: %NGINX_DIR%

:: 检查nginx目录是否存在
if not exist "%NGINX_DIR%" (
    echo 错误: Nginx目录不存在: %NGINX_DIR%
    pause
    exit /b 1
)

:: 创建目标目录
echo 创建目标目录...
if not exist "%NGINX_DIR%\html" mkdir "%NGINX_DIR%\html"
if not exist "%NGINX_DIR%\html\frontend-admin" mkdir "%NGINX_DIR%\html\frontend-admin"
if not exist "%NGINX_DIR%\html\frontend-h5" mkdir "%NGINX_DIR%\html\frontend-h5"

:: 复制管理员前端
echo.
echo 复制管理员前端文件...
if exist "%PROJECT_DIR%frontend-admin\dist" (
    echo 清理旧文件...
    if exist "%NGINX_DIR%\html\frontend-admin\*" (
        del /Q "%NGINX_DIR%\html\frontend-admin\*.*"
        for /d %%i in ("%NGINX_DIR%\html\frontend-admin\*") do rd /s /q "%%i"
    )
    
    echo 复制新文件...
    xcopy "%PROJECT_DIR%frontend-admin\dist\*" "%NGINX_DIR%\html\frontend-admin\" /E /Y /I
    if errorlevel 1 (
        echo 错误: 管理员前端文件复制失败
        pause
        exit /b 1
    )
    echo 管理员前端部署完成 ✓
) else (
    echo 警告: 管理员前端dist目录不存在，请先运行构建
)

:: 复制H5前端
echo.
echo 复制H5前端文件...
if exist "%PROJECT_DIR%frontend-h5\dist" (
    echo 清理旧文件...
    if exist "%NGINX_DIR%\html\frontend-h5\*" (
        del /Q "%NGINX_DIR%\html\frontend-h5\*.*"
        for /d %%i in ("%NGINX_DIR%\html\frontend-h5\*") do rd /s /q "%%i"
    )
    
    echo 复制新文件...
    xcopy "%PROJECT_DIR%frontend-h5\dist\*" "%NGINX_DIR%\html\frontend-h5\" /E /Y /I
    if errorlevel 1 (
        echo 错误: H5前端文件复制失败
        pause
        exit /b 1
    )
    echo H5前端部署完成 ✓
) else (
    echo 警告: H5前端dist目录不存在，请先运行构建
)

:: 复制nginx配置文件
echo.
echo 更新nginx配置文件...
if exist "%PROJECT_DIR%conf\nginx-dhi-exam.conf" (
    copy "%PROJECT_DIR%conf\nginx-dhi-exam.conf" "%NGINX_DIR%\conf\nginx.conf" /Y
    if errorlevel 1 (
        echo 错误: nginx配置文件复制失败
        pause
        exit /b 1
    )
    echo nginx配置文件更新完成 ✓
) else (
    echo 警告: nginx配置文件不存在
)

echo.
echo ================================
echo 部署完成！
echo ================================
echo 管理后台: http://localhost:3001/admin/
echo H5前端: http://localhost:3001/h5/
echo API接口: http://localhost:3001/api/
echo.
echo 下一步:
echo 1. 启动后端服务 (在backend目录运行 npm start)
echo 2. 启动或重载nginx (运行 start-nginx.bat)
echo ================================

pause
